﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Management;

namespace WINADM.Server.WMI
{
    public class Win32EventLogHelper : IWin32EventLogHelper
    {
        ILog _logger;
        public Win32EventLogHelper(ILog logger)
        {
            _logger = logger;
        }

        public IList<Win32EventLog> GetAllEventLogEntries(DateTime fromDate)
        {
            IList<Win32EventLog> eventLogEntries = new List<Win32EventLog>();
            try
            {
                string DTMFDate = WMIUtility.ConvertDateTimeToDmtf(fromDate);
                ManagementObjectSearcher searcher =
                           new ManagementObjectSearcher(
                           WMIConstants.WMIRootNamespace,
                           String.Format(WMIConstants.WMIQuery_AllEventLogEntry, DTMFDate));

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    eventLogEntries.Add(new Win32EventLog(queryObj));
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error occured while fetching event log information", ex);
            }

            return eventLogEntries;
        }
    }
}
